<template>
  <!-- 行车管理-月卡管理 -->
  <div>
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input v-model="params.carNumber" placeholder="请输入车牌号码" class="search-main" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="params.personName" placeholder="请输入车主姓名" class="search-main" />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus" placeholder="未选择" class="search-main">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="doSearch">查询</el-button>
    </div>

    <!-- 操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/car/addMonthCard')">添加月卡</el-button>
      <el-button type="primary" @click="delCartList">批量删除</el-button>
    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="list" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column type="index" label="序号" :index="indexMethod" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="phoneNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="状态" prop="cardStatus" :formatter="formatStatus" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text">续费</el-button>
            <el-button size="mini" type="text">查看</el-button>
            <el-button size="mini" type="text" @click="editCard(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="deleteCard(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    分页-->
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :page-size="params.pageSize"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 添加楼宇 -->
    <el-dialog v-auth-btn="'park:building:add_edit'" title="添加楼宇" width="580px">
      <!-- 表单接口 -->
      <div class="form-container" />
      <template #footer>
        <el-button size="mini">取 消</el-button>
        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getCartListAPI, delCardAPI } from '@/api/cart'
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 2,
        carNumber: null,
        personName: null,
        cardStatus: null
      },
      list: [],
      total: 0,
      options: [{
        value: null,
        label: '全部'
      }, {
        value: 0,
        label: '可用'
      }, {
        value: 1,
        label: '不可用'
      }],
      // 已选择列表
      selectedCarList: []
    }
  },
  created() {
    this.getCartList()
  },
  methods: {
    async getCartList() {
      const res = await getCartListAPI(this.params)
      this.list = res.data.rows
      this.total = res.data.total
    },
    formatStatus(row) {
      const MAP = {
        0: '可用',
        1: '不可用'
      }
      return MAP[row.cardStatus]
    },
    handleCurrentChange(val) {
      console.log(`当前页:${val}`)
      this.params.page = val
      this.getCartList()
    },
    indexMethod(row) {
      return (this.params.page - 1) * this.params.pageSize + 1 + row
    },
    doSearch() {
      // 调用前把页数参数重置为1
      this.params.page = 1
      this.getCartList()
    },
    editCard(id) {
      this.$router.push({
        path: '/car/addMonthCard',
        query: {
          id
        }
      })
    },
    async deleteCard(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delCardAPI(id)
        if (this.list.length === 1 && this.params.page > 1) {
          this.params.page = 1
        }
        await this.getCartList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSelectionChange(rowList) {
      this.selectedCarList = rowList
    },
    delCartList() {
      if (this.selectedCarList.length <= 0) {
        this.$message.warning('还未选中要删除的数据')
        return
      }
      this.$confirm('此操作将永久删除选择的月卡, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 批量删除时，id用逗号隔开
        const ids = this.selectedCarList.map(item => item.id)
        await delCardAPI(ids.join(','))
        await this.getCartList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  // 设置弹性盒子
  display: flex;
  // 在交叉轴上居中对齐，将在容器的垂直方向上居中对齐
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>
